$(document).ready(function() {
	if (parent.window != window) { // 含有父窗体
		parent.window.location.href = "${ctx}/";
	}
});

var mainUrl = getHttpRequestPath('/main/welcome');

$(function() {
	getMenuUrlList();
	
	$('#tabs').tabs('add', {
		title : '欢迎使用',
		content : createFrame(mainUrl)
	});
});

function getMenuUrlList() {
	$.post(getHttpRequestPath('/main/getMenuUrl'), {
		
	}, function(typesString) {
		menus = JSON.parse(typesString, function(key, value) {
			return value;
		});
		initLeftMenu(menus);
	});
}

function initLeftMenu(menus) {
	$("#nav").accordion({
		animate:false
	});
	for (var i = 0; i < menus.length; i++) {
		if (menus[i].menuCode == menus[i].parentCode) {
			var menuList = '';
			menuList += '<ul>';
			for (var j = 0; j < menus.length; j++) {
				if (menus[j].parentCode == menus[i].menuCode && menus[j].menuCode != menus[j].parentCode) {
					menuList += '<li><div><a ref="' + menus[j].menuCode + '" href="#" rel="' + getHttpRequestPath(menus[j].menuUrl) + '"><span class="nav">' + menus[j].menuName + '</span></a></div></li>';
				}
			}
			menuList += '</ul>';
			$('#nav').accordion('add', {
				title : menus[i].menuName,
				content : menuList
			});
		}
	}
	$('.easyui-accordion li a').click(function() {
		var tabTitle = $(this).children('.nav').text();

		var url = $(this).attr("rel");
		addTab(tabTitle, url, '');
		$('.easyui-accordion li div').removeClass("selected");
		$(this).parent().addClass("selected");
	}).hover(function() {
		$(this).parent().addClass("hover");
	}, function() {
		$(this).parent().removeClass("hover");
	});
}

function addTab(subtitle, url, icon) {
	if (!$('#tabs').tabs('exists', subtitle)) {
		$('#tabs').tabs('add', {
			title : subtitle,
			content : createFrame(url),
			closable : true,
			icon : icon
		});
	} else {
		$('#tabs').tabs('select', subtitle);
	}
}

function createFrame(url) {
	var s = '<iframe scrolling="auto" frameborder="0" src="' + url
			+ '" style="width:100%;height:100%;"></iframe>';
	return s;
}

function editPwd() {
	$('#dlg').dialog({
		modal : true
	}).dialog('open').dialog('setTitle', '修改密码');
	$("#pwdFrame").attr("src", "${ctx}/profile");
}

function logout() {
	window.location.href = "${ctx}/logout";
}

function formatNowDate() {
	var today = new Date();
	var year = today.getFullYear();
	var month = today.getMonth() + 1;
	var day = today.getDate();
	var hrs = today.getHours();
	var min = today.getMinutes();
	var sec = today.getSeconds();

	if (month < 10) {
		month = "0" + month;
	}
	if (day < 10) {
		day = "0" + day;
	}
	if (hrs < 10) {
		hrs = "0" + hrs;
	}
	if (min < 10) {
		min = "0" + min;
	}
	if (sec < 10) {
		sec = "0" + sec;
	}

	nowtime.innerHTML = year + "年" + month + "月" + day + "日&nbsp;&nbsp;&nbsp"
			+ hrs + ":" + min + ":" + sec + "&nbsp;&nbsp;&nbsp;星期"
			+ "日一二三四五六".charAt(today.getDay());
}
setInterval("formatNowDate()", 1000);